<template>
  <div>
    <div class="ui container segment">
      <el-table :data="pendingFileLists.slice((currentPage-1)*pageSize,currentPage*pageSize)" style="width: 100%" v-loading="isloading" fit  >
        <el-table-column prop="realName" label="文件名" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="uploadTime" :formatter="dateFormatter" label="上传时间">
        </el-table-column>
        <el-table-column prop="description" label="描述" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="nickname" label="上传者">
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="150" align="center">
          <template slot-scope="scope">
            <el-button @click="docPreview(scope.row)" type="text">预览</el-button>
            <el-button @click="passAudit(scope.row)" type="text">通过</el-button>
            <el-button @click="quitAudit(scope.row)" type="text">驳回</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页器 -->
      <div class="block" style="margin-top:15px;">
        <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage"
          :page-sizes="[10, 15, 20, 25]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total">
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import {view} from '../../utils/func'
export default {
  inject: ['reload'],
  data() {
    return {
      isloading: false,                  //文件处理加载框
      pendingFileLists: null,           //需要审批的文件
      currentPage: 1, // 当前页码
      total: 20, // 总条数
      pageSize: 2 // 每页的数据条数
    }
  },
  methods: {
    docPreview(row){
      view(row);      //预览文件
    },
    getPendingList() {
      this.$https.get('/admin/pendingFileList').then((res) => {
        if (res.data.code == 200)
          this.pendingFileLists = res.data.data;
        else
          this.$message.info(res.data.message);
      })
    },
    passAudit(row) {
      this.isloading = true;
      this.$https({
        method: 'post',
        url: '/admin/approvedFile',
        params: {
          fileId: row.id,
        }
      }).then(res => {
        console.log(res);
        if (res.data.code == 200) {
          this.isloading = false;
          this.$message.success(res.data.data);
          this.reload();
        } else {
          this.isloading = false;
          this.$message.error(res.data.data);
          this.reload();
        }
      })
    },
    quitAudit(row) {
      this.isloading = true;
      console.log(row.id);
      this.$https({
        method: 'post',
        url: '/admin/overruleFile',
        params: {
          fileId: row.id,
        }
      }).then(res => {
        console.log(res);
        if (res.data.code == 200) {
          this.isloading = false;
          this.$message.success(res.data.data);
          this.reload();
        } else {
          this.isloading = false;
          this.$message.error(res.data.data);
          this.reload
        }
      })
    },
    dateFormatter(row, column) {
      let datetime = row.uploadTime;
      return this.timestampToTime(datetime);
    },
    timestampToTime(time) {
      //时间戳转换
      // 时间戳为10位需*1000，时间戳为13位的话不需乘1000
      var date = new Date(time);
      let y = date.getFullYear();
      let MM = date.getMonth() + 1;
      let d = date.getDate();
      let h = date.getHours();
      let m = date.getMinutes();
      let s = date.getSeconds();
      MM = MM < 10 ? "0" + MM : MM;
      d = d < 10 ? "0" + d : d;
      h = h < 10 ? "0" + h : h;
      m = m < 10 ? "0" + m : m;
      s = s < 10 ? "0" + s : s;
      // return y + "-" + MM + "-" + d + " " + h + ":" + m + ":" + s;
      return y + "-" + MM + "-" + d;
    },
    //每页条数改变时触发 选择一页显示多少行
    handleSizeChange(val) {
      this.currentPage = 1;
      this.pageSize = val;
    },
    //当前页改变时触发 跳转其他页
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getPendingList(val);
    },
  },
  mounted() {
    this.getPendingList();
  },
}
</script>

<style>
</style>